package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.platform.entity.Element;
import com.vf.admin.platform.entity.RoleMenu;
import com.vf.admin.platform.entity.RoleMenuElement;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class RoleMenuElementDao extends BaseDao<RoleMenuElement>{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 保存或者更新
     */
    public void saveOrUpdate(RoleMenuElement roleMenuElement){
        this.add(roleMenuElement);
    }

    //列表查询
    public List<RoleMenuElement> findList(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_role_menu_element  where 1=1");
        if(!StringEx.isNullOrLing(paraMap.get("roleid"))){
            sbStr.append(" and roleid = ? ");
            paramList.add(paraMap.get("roleid"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("menuid"))){
            sbStr.append(" and menuid = ? ");
            paramList.add(paraMap.get("menuid"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("elementid"))){
            sbStr.append(" and elementid = ? ");
            paramList.add(paraMap.get("elementid"));
        }
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,RoleMenuElement.class);
    }

    /**
     * 为菜单分配的标签权限
     *
     * @return
     */
    public void save(List<RoleMenuElement> roleMenuElementList) {
        try {
            if(roleMenuElementList == null || roleMenuElementList.size()<=0){
                return ;
            }
            ArrayList<Object> paramList = new ArrayList<Object>();
            StringBuilder sbSql = new StringBuilder("insert into bt_role_menu_element (roleid,menuid,elementid)  values");
            for (RoleMenuElement roleMenuElement : roleMenuElementList) {
                if (roleMenuElement == null) {
                    continue;
                }
                sbSql.append(" (?,?,?) ,");
                paramList.add(roleMenuElement.getRoleid());
                paramList.add(roleMenuElement.getMenuid());
                paramList.add(roleMenuElement.getElementid());
            }
            String sql = sbSql.substring(0,sbSql.lastIndexOf(","));
            this.update(sql, paramList);
        } catch (Exception e) {
            logger.error("为菜单分配的标签权限时异常",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除
     */
    public void del(Integer roleid){
        if (roleid == null || roleid.intValue() <= 0) {
            return;
        }
        String sql = "delete  from bt_role_menu_element where roleid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(roleid);
        this.del(sql, paramList);
    }

    /**
     * 删除 根据菜单id
     */
    public void delByMenuId(Integer menuid){
        if (menuid == null || menuid.intValue() <= 0) {
            return;
        }
        String sql = "delete  from bt_role_menu_element where menuid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(menuid);
        this.del(sql, paramList);
    }

    /**
     * 删除 根据角色id
     */
    public void delByRoleId(Integer roleid){
        if (roleid == null || roleid.intValue() <= 0) {
            return;
        }
        String sql = "delete  from bt_role_menu_element where roleid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(roleid);
        this.del(sql, paramList);
    }

    /**
     * 删除 根据角色id
     */
    public void delByElementId(Integer elementid){
        if (elementid == null || elementid.intValue() <= 0) {
            return;
        }
        String sql = "delete  from bt_role_menu_element where elementid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(elementid);
        this.del(sql, paramList);
    }

    //列表用户拥有的所有的标签对应的接口权限
    public List<Element> findElementListByRoleMenurId(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder(" SELECT be.* FROM bt_element be WHERE be.id IN (SELECT brme.elementid FROM bt_role_menu_element brme WHERE 1 = 1   ");
        if(StringEx.isNotNull(paraMap.get("roleid"))){
            sbStr.append(" and brme.roleid = ? ");
            paramList.add(paraMap.get("roleid"));
        }
        if(StringEx.isNotNull(paraMap.get("menuid"))){
            sbStr.append(" and brme.menuid = ? ");
            paramList.add(paraMap.get("menuid"));
        }
        sbStr.append(" ) ");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, Element.class);
    }

    //列表用户拥有的所有的标签对应的接口权限
    public List<Element> findElementListByRoleId(List<RoleMenu> roleMenuList){
        StringBuilder sbRoleId = new StringBuilder("0");
        if(roleMenuList!=null && roleMenuList.size()>0){
            for(RoleMenu roleMenu : roleMenuList){
                sbRoleId.append(","+roleMenu.getRoleid());
            }
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder(" SELECT be.* FROM bt_element be WHERE be.id IN (SELECT brme.elementid FROM bt_role_menu_element brme WHERE 1 = 1   ");
        sbStr.append(" and FIND_IN_SET (brme.roleid,?) ");
        String paraStr = sbRoleId.toString();
        paramList.add(paraStr);

        sbStr.append(" ) ");
        String sql = sbStr.toString();
        List list = this.queryList(sql,paramList, Element.class);

        return list;
    }
}
